// pages/aduting/retreat/retreat_list.js
var util = require('../../../utils/util.js');
var app = getApp();
var limit = 15;// 每页显示的条数
var today = util.formatTime(new Date());
var minday = util.formatTime(new Date());
var maxday = util.formatTime(new Date());
var listArray = [];//公寓list
var roomArray = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    apartment_id: '',
    apartment_name: '',
    room_id: '-1',
    check_state: '0',
    keyword: '',
    beginDate: '',
    endDate: '',
    date: today,
    minday: today,
    maxday: maxday,
    apartmentArray: '',
    room_name: '',
    roomArray: '',
    stateArray: [{ "key": "待我审批", "value": '0' }, { "key": "待租户同意", "value": '2' }],
    stateName: '',
    url: '/pages/aduting/retreat/retreat_detail',
    nomore: false,
    hiddenmodalput: true,
    time1: '开始日期',
    time2: '结束日期',
    apartment_name1: '楼栋',
    room_name1: '房间号',
    stateName1: '审批状态',
    scrollTop: 0,//设置srcoll-view距离顶部位置
    scrollHeight: 0,
    dataList: [],
    currentPage:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // 上拉加载更多
  loadMore: function (e) {
    var that = this;
    if (!that.data.nomore) {
      that.setData({
        currentPage: that.data.currentPage+1
      })
      util.loading();
      that.getDataofres(that.data.userInfo);
    }

  },
   /** 下拉刷新
  refresh: function (e) {
    that.setData({
      currentPage: 1
    })
    this.setData({
      nomore: false,
      dataList: [],
      scrollTop: 0
    })
    this.getDataofres(this.data.userInfo);
  },
  /**
* 滑动事件
* */
  scroll: function (e) {
    var that = this;
    that.setData({
      top: e.detail.scrollTop
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow:function(options){
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight - 20
        });
      }
    });
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        that.setData({
          userInfo: res.data,
          dataList:[]
        })

        that.getDataofres(res.data);
        that.getApartment(res.data);
        that.getDataofRoom(res.data);
      }, fail: function () {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    });

  },
  onLoad: function (options) {
    
  },
  getDataofres: function (data) {
    var that = this
    util.getReq("/app/my/checkOutList", {
      source: data.source,
      uid: data.user_id,
      'term.pageIndex': that.data.currentPage,
      "term.limit": limit,
      'term.apartment_id': that.data.apartment_id,
      'term.room_id': that.data.room_id,
      'term.check_state': that.data.check_state,
      'term.keyword': that.data.keyword,
      'term.beginDate': that.data.beginDate,
      'term.endDate': that.data.endDate
    }, function (datas) {
      util.closeloading();
      wx.stopPullDownRefresh();
      var dataLists = that.data.dataList;
 
      if (datas.code == 0) {
        if (datas.data.data) {
          var len = datas.data.data.length;
          console.log(len);
          for (var i = 0; i < len; i++) {

            dataLists.push(datas.data.data[i]);
          }
        }
        console.log(dataLists);
        that.setData({
          dataList: dataLists
        })
        console.log(that.data.dataList);
        if (datas.data.pageCount <= that.data.currentPage) {
          that.setData({
            nomore: true
          })

        }
      } else {
        wx.showToast({
          title: datas.desc,
        })
      }
    });

  },
  //选择公寓
  bindPartmentChange: function (e) {
    var str = "";
    if (this.data.apartmentArray[e.detail.value].key.length > 5) {
      str = this.data.apartmentArray[e.detail.value].key.substring(0, 5) + "...";
    } else {
      str = this.data.apartmentArray[e.detail.value].key;
    }
    this.setData({
      apartment_id: this.data.apartmentArray[e.detail.value].value,
      apartment_name: str,
      apartment_name1: str,
      room_name: '',
      roomArray: '',
      room_id: '-1',
      room_name1: '房间号',
    })
    this.getDataofRoom(this.data.userInfo);

  },
  //获取公寓
  getApartment: function (data) {
    var that = this;
    util.getReq("/app/home/allApartmentInterface", {
      source: data.source,
      uid: data.user_id
    }, function (datas) {

      if (datas.code == 0) {
        var list = datas.params.list;
        var lth = list.length;
        listArray = [];
        if (lth > 0) {
          for (var i = 0; i < lth; i++) {
            var ob = new Object();
            ob.key = list[i].apartment_name;
            ob.value = list[i].apartment_id;
            listArray.push(ob);

          }

        }
        that.setData({
          apartmentArray: listArray

        })
      } else {
        wx.showToast({
          title: datas.desc,
        })
      }

    })
  },
  //获取房间
  getDataofRoom: function (data) {
    var that = this;
    util.getReq("/app/home/allRoomInterface", {
      source: data.source,
      uid: data.user_id,
      apartment_id: that.data.apartment_id
    }, function (datas) {
      if (datas.code == 0) {
        var list = datas.params.list;
        var lth = list.length;
        roomArray = new Array();
        if (lth > 0) {
          for (var i = 0; i < lth; i++) {
            roomArray.push({ "key": list[i].room_code, "value": list[i].id });
          }

        }
        that.setData({
          roomArray: roomArray
        })
      } else {
        wx.showToast({
          title: datas.desc,
        })
      }

    })
  },
  //选择房间
  bindRoomChange: function (e) {
    this.setData({
      room_id:  this.data.roomArray[e.detail.value].value,
      room_name: this.data.roomArray[e.detail.value].key,
      room_name1: this.data.roomArray[e.detail.value].key
    })
  },
  //状态选择
  bindStateChange: function (e) {
    this.setData({
      stateName: this.data.stateArray[e.detail.value].key,
      check_state: this.data.stateArray[e.detail.value].value,
      stateName1: this.data.stateArray[e.detail.value].key
    })

  },
  //选择开始日期
  bindDateStartChange: function (e) {
    this.setData({
      beginDate: e.detail.value,
      time1: e.detail.value
    })
  },
  //选择结束日期
  bindDateEndChange: function (e) {
    this.setData({
      endDate: e.detail.value,
      time2: e.detail.value
    })
  },

  //取消时不显示
  termCel: function (e) {
    this.setData({
      hiddenmodalput: true,
      apartment_id: '',
      apartment_name: '',
      room_id: '-1',
      check_state: '',
      keyword: '',
      beginDate: '',
      endDate: '',
      time1: '开始日期',
      time2: '结束日期',
      apartment_name1: '请选择楼栋',
      room_name1: '请选择房间号',
      stateName1: '请选择审批状态',
    })

  },
  //更多打开
  moreTap: function (e) {
    this.setData({
      hiddenmodalput: false

    })
  },
  //确定
  termConfirm: function (e) {
    this.setData({
      hiddenmodalput: true,
     nomore: false,
     currentPage:1,
    })
    this.bindSearch(e);
  },
  bindSearch: function (e) {
    this.setData({
      nomore: false,
      dataList: [],
      currentPage: 1,
    })
    this.getDataofres(this.data.userInfo);
  },
  //输入框
  wxSearchInput: function (e) {

    this.setData({

      "wxSearchData.value": e.detail.value,
      keyword: e.detail.value
    })
  }

})